#ifndef PAGE_ENVMONITOR_H
#define PAGE_ENVMONITOR_H

#include <QWidget>
#include <QCoreApplication>
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <QDebug>
#include <QTimer>
#include "qcustomplot.h"
#include "history.h"
#include <QtSql>

namespace Ui {
class Page_envMonitor;
}

class Page_envMonitor : public QWidget
{
    Q_OBJECT

public:
    explicit Page_envMonitor(QWidget *parent = nullptr);
    ~Page_envMonitor();

signals:
    void backToMainPage();

private slots:
    void on_pBtn_back_clicked();
    void on_pushButton_clicked();
    //void on_pushButton_2_clicked();
    void on_pushButton_3_clicked();
    void on_pushButton_4_clicked();
    void setupPlots();
    void sendCommand1();
    void serialPort_readyRead();  //串口接收

    void on_setting_clicked();

private:
    Ui::Page_envMonitor *ui;
    QSerialPort *serial;
    QTimer *timer;
    QCPGraph *temperatureGraph;
    QCPGraph *humidityGraph;
    QCPGraph *illuminationGraph;
    QTime startTime1;

    history *m_history;

    QSqlDatabase db;
    QDateTime startTime;
    QDateTime lastTimestamp;
    QString currentTableName;

    void createNewTable(const QDateTime &timestamp);
    void updateMetaData(const QString &tableName, const QDateTime &endTime);
    void recordSensorData(const QDateTime &timestamp, double temperature, double humidity, double illumination);
};

#endif // PAGE_ENVMONITOR_H
